using System;
using System.Xml;
using UnityEngine;

namespace AssemblyCSharp
{
	public class PlanetDAO : XmlParserDAO, CommonDAOIF<PlanetDomain>
	{
		private PlanetDomain mPlanetDomain = null;
		
		public bool readStarXml(String xmlString)
		{
			return mXmlParser.readXmlString(xmlString);
		}
		
		public override System.Object parse()
		{
			try
			{
				String returnValue = "";
				mPlanetDomain = new PlanetDomain();
				
				foreach(XmlNode item in mXmlParser.xmlDoc.ChildNodes)
				{
					GetChildren(item);
				}
				
				return returnValue;
			}
			catch ( Exception e )
			{
				Debug.Log( e.ToString());
				return null;
			}
		}

		private RevolutionDAO mRevolutionDAO = new RevolutionDAO();
		private RotationDAO mRotationDAO = new RotationDAO();
		private void GetChildren(XmlNode node)
		{
			if (node.LocalName == Constant.PlanetName) {
				mPlanetDomain.Name = node.InnerText;
			} else if (node.LocalName == Constant.PlanetRadius) {
				mPlanetDomain.Radius = Convert.ToInt32(node.InnerText);
			} else if (node.LocalName == Constant.PlanetRevolution) {
				// Call Revolution DAO
				mRevolutionDAO.readStarXml(node.OuterXml);
				mRevolutionDAO.parse();
				RevolutionDomain mRevolutionDomain = mRevolutionDAO.getDomain();
				mPlanetDomain.RevolutionDomain = mRevolutionDomain;
			} else if (node.LocalName == Constant.PlanetRotation) {
				// Call Rotation DAO
				mRotationDAO.readStarXml(node.OuterXml);
				mRotationDAO.parse();
				RotationDomain mRotationDomain = mRotationDAO.getDomain();
				mPlanetDomain.RotationDomain = mRotationDomain;
			}
			else
			{
				foreach (XmlNode item in node.ChildNodes)
				{
					GetChildren(item);
				}
			}
		}
		
		public PlanetDomain getDomain ()
		{
			return mPlanetDomain;
		}
	}
}

